layui.use(['form', 'eleTree', 'fsoft'], function () {
  var $ = layui.$
    , eleTree = layui.eleTree
    , form = layui.form
    , fsoft = layui.fsoft;
  var roleId = $("#id").val();
  var roleTree = eleTree.render({
    elem: '#roleMenuTree'
    , defaultExpandedKeys: ["7E7EADD37FB346E080CA719A5748EBD5", "2C54F76024DD4CE1B4F678E6E1A69125"]
    , showCheckbox: true
    , showLine: true
    , url: layui.cache['contentPath'] + '/sys-role/findRoleMenuTrees'
    , method: 'post'
    , contentType: 'application/json'
    , where: { "roleId": roleId }
    , request: { name: "title" }
    , response: { dataName: "menutree" }
    , done: function (res) {

      if (res.code == 0) {
        if (res.rolemenu != undefined) {
          var arrs = [];
          $.each(res.rolemenu, function (idx, item) {
            if (item.isLeaf == 1)
              arrs.push(item.id);
          });
          roleTree.setChecked(arrs);
        }
        if (res.menutree != undefined && res.menutree.length > 0) {
          var data = res.menutree;
          roleTree.expandNode(data[0].id)
        }
      }
    }
  });
  // 监听提交
  form.on('submit(save)', function (data) {
    var checkData = roleTree.getChecked(false, true);
    var menus = [];
    $.each(checkData, function (idx, item) {
      menus.push(item.id);
    });
    var params = data.field;
    params["menus"] = menus;
    fsoft.confirm('询 问', "确认要修改权限？", function (index) {
      fsoft.put(layui.cache['contentPath'] + '/sys-role/modify', params, function (rs) {
        if (rs.code == 0) {
          fsoft.successMsg("设置权限成功!");
          xadmin.close();
        } else {
          fsoft.errorMsg(rs.msg);
        }
      });
      layer.close(index);
    });
    return false;
  });

});